﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;
using System.Data;

namespace stata
{
    /// <summary>
    /// Interaction logic for Wykresy.xaml
    /// </summary>
    public partial class Wykresy : Window
    {

        DataTable dtMethodOne;
        DataTable dtEmpty;
        DataTable dtOriginal;
        DataTable dtMethodTwo;
        public static Operations oper;
        public Wykresy(Operations op)
        {


            InitializeComponent();


        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            double FirstMetError = 0;
            int emptiesCount = 0;
            double SecondMetError = 0;
            //chart1.Series.Clear();
            oper.dt = dtEmpty;
            int kolumna = -1;
            if (comboBox3.SelectedItem.ToString() != "Wszystkie")
            {
                dtMethodOne = new DataTable();
                dtMethodTwo = new DataTable();
                for (int i = 0; i < dtEmpty.Columns.Count; i++)
                {
                    dtMethodOne.Columns.Add(new DataColumn(dtEmpty.Columns[i].ColumnName));
                    dtMethodTwo.Columns.Add(new DataColumn(dtEmpty.Columns[i].ColumnName));
                }
                for (int i = 0; i < dtEmpty.Rows.Count; i++)
                {
                    string[] tab = new string[dtEmpty.Columns.Count];
                    dtMethodOne.Rows.Add(tab);
                    dtMethodTwo.Rows.Add(tab);
                }
                for (int i = 0; i < dtEmpty.Columns.Count; i++)
                {
                    if (dtEmpty.Columns[i].ColumnName.Equals(comboBox3.SelectedItem.ToString()))
                    {
                        kolumna = i;

                    }
                }
                //dtMethodOne = dtOriginal;

                for (int j = 0; j < dtEmpty.Rows.Count; j++)
                {
                    if (dtEmpty.Rows[j].Field<string>(kolumna) == "?")
                    {
                        dtMethodOne.Rows[j].SetField<string>(kolumna, oper.Abs(dtEmpty.Columns.Count - 1, kolumna, j).ToString());
                    }
                }
                //dtMethodTwo = dtOriginal;

                for (int j = 0; j < dtEmpty.Rows.Count; j++)
                {
                    List<String> nowy = new List<string>();
                    if (dtEmpty.Rows[j].Field<string>(kolumna) == "?")
                    {
                        for (int jj = 0; jj < dtEmpty.Columns.Count; jj++)
                        {
                            try
                            {
                                String wiersz = dtEmpty.Rows[j].Field<String>(jj);
                                wiersz = wiersz.Replace('.', ',');
                                float w = float.Parse(wiersz);

                                nowy.Add(wiersz);
                            }
                            catch (Exception ee)
                            {

                            }
                        }
                        dtMethodTwo.Rows[j].SetField<string>(kolumna, oper.klasyfikacjaEuklides(nowy, kolumna, Int32.Parse(textBox1.Text), j).ToString());
                    }
                }
                for (int i = 0; i < dtEmpty.Rows.Count; i++)
                {
                    if (dtEmpty.Rows[i].Field<string>(kolumna) == "?")
                    {
                        emptiesCount++;
                        double wyn = Double.Parse(dtMethodOne.Rows[i].Field<string>(kolumna))-Double.Parse(dtOriginal.Rows[i].Field<string>(kolumna));
                        wyn = wyn*wyn;
                        wyn = Math.Sqrt(wyn);
                        FirstMetError += wyn;
                        wyn = Double.Parse(dtMethodTwo.Rows[i].Field<string>(kolumna))-Double.Parse(dtOriginal.Rows[i].Field<string>(kolumna));
                        wyn = wyn*wyn;
                        wyn = Math.Sqrt(wyn);
                        SecondMetError += wyn;
                    }
                }
            }
            else
            {
                dtMethodOne = new DataTable();
                dtMethodTwo = new DataTable();
                for (int i = 0; i < dtEmpty.Columns.Count; i++)
                {
                    dtMethodOne.Columns.Add(new DataColumn(dtEmpty.Columns[i].ColumnName));
                    dtMethodTwo.Columns.Add(new DataColumn(dtEmpty.Columns[i].ColumnName));
                }
                for (int i = 0; i < dtEmpty.Rows.Count; i++)
                {
                    string[] tab = new string[dtEmpty.Columns.Count];
                    dtMethodOne.Rows.Add(tab);
                    dtMethodTwo.Rows.Add(tab);
                }
                    for (int i = 0; i < dtEmpty.Columns.Count; i++)
                    {
                        // dtMethodOne.Columns.Add(new DataColumn(dtEmpty.Columns[i].ColumnName));
                        for (int j = 0; j < dtEmpty.Rows.Count; j++)
                        {
                            dtMethodOne.Rows[j].SetField<string>(i, dtEmpty.Rows[j].Field<string>(i));
                            dtMethodTwo.Rows[j].SetField<string>(i, dtEmpty.Rows[j].Field<string>(i));
                        }
                    }
                    for (int i = 0; i < dtEmpty.Columns.Count - 1; i++)
                        for (int j = 0; j < dtEmpty.Rows.Count; j++)
                        {
                            if (dtEmpty.Rows[j].Field<string>(i) == "?")
                            {
                                dtMethodOne.Rows[j].SetField<string>(i, oper.Abs(dtEmpty.Columns.Count - 1, i, j).ToString());
                            }
                        }
                //dtMethodTwo = dtEmpty;
                for (int i = 0; i < dtEmpty.Columns.Count - 1; i++)
                for (int j = 0; j < dtEmpty.Rows.Count; j++)
                {
                    List<String> nowy = new List<string>();
                    if (dtEmpty.Rows[j].Field<string>(i) == "?")
                    {
                        for (int jj = 0; jj < dtEmpty.Columns.Count; jj++)
                        {
                            try
                            {
                                String wiersz = dtEmpty.Rows[j].Field<String>(jj);
                                wiersz = wiersz.Replace('.', ',');
                                float w = float.Parse(wiersz);

                                nowy.Add(wiersz);
                            }
                            catch (Exception ee)
                            {

                            }
                        }
                        dtMethodTwo.Rows[j].SetField<string>(i, oper.klasyfikacjaEuklides(nowy, i, Int32.Parse(textBox1.Text), j).ToString());
                    }
                }
                for (int j = 0; j < dtEmpty.Columns.Count; j++ )
                    for (int i = 0; i < dtEmpty.Rows.Count; i++)
                    {
                        if (dtEmpty.Rows[i].Field<string>(j) == "?")
                        {
                            emptiesCount++;
                           double wyn = Double.Parse(dtMethodOne.Rows[i].Field<string>(j))-Double.Parse(dtOriginal.Rows[i].Field<string>(j));
                        wyn = wyn*wyn;
                        wyn = Math.Sqrt(wyn);
                        FirstMetError += wyn;
                        wyn = Double.Parse(dtMethodTwo.Rows[i].Field<string>(j))-Double.Parse(dtOriginal.Rows[i].Field<string>(j));
                        wyn = wyn*wyn;
                        wyn = Math.Sqrt(wyn);
                        SecondMetError += wyn;
                        }
                    }
            }
            FirstMetError = FirstMetError / emptiesCount;
            SecondMetError = SecondMetError / emptiesCount;

                
                ColumnSeries ser1 = new ColumnSeries();
                
               
                ser1.DependentValuePath = "Value";
                ser1.IndependentValuePath = "Key";
                columnChart.Series.Add(ser1);
                List<KeyValuePair<string,double>> l = new List<KeyValuePair<string,double>>();
                KeyValuePair<string,double> p = new KeyValuePair<string,double>("srednia",FirstMetError);
                KeyValuePair<string, double> p2 = new KeyValuePair<string, double>("KNN", SecondMetError);
                l.Add(p2);
                KeyValuePair<string, double> p3 = new KeyValuePair<string, double>("SVD", SecondMetError);
                l.Add(p3);
                l.Add(p);
                ((ColumnSeries)columnChart.Series[0]).ItemsSource = l;
              
               
                
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            oper = new Operations();
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            // Set filter for file extension and default file extension


            // Display OpenFileDialog by calling ShowDialog method
            Nullable<bool> result = dlg.ShowDialog();

            // Get the selected file name and display in a TextBox
            if (result == true)
            {


                // Open document
                string filename = dlg.FileName;
                string sciezka = filename;
                nazwy n = new nazwy(sciezka, oper);
                n.ShowDialog();
                dtOriginal = oper.dt;
            }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            oper = new Operations();
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            // Set filter for file extension and default file extension


            // Display OpenFileDialog by calling ShowDialog method
            Nullable<bool> result = dlg.ShowDialog();

            // Get the selected file name and display in a TextBox
            if (result == true)
            {


                // Open document
                string filename = dlg.FileName;
                string sciezka = filename;
                nazwy n = new nazwy(sciezka, oper);
                n.ShowDialog();
                dtEmpty = oper.dt;
                object[] wier = dtEmpty.Rows[0].ItemArray;
                comboBox3.Items.Add("Wszystkie");
                for (int z = 0; z < wier.Length; z++)
                {
                    string s = wier[z].ToString();
                    comboBox3.Items.Add(dtEmpty.Columns[z].ColumnName);
                    s = s.Replace('.', ',');
                }

                comboBox3.Items.Refresh();
            }
        }
    }
}
